import axios from 'axios';

const API_KEY = 'YOUR_API_KEY'; // 替换为您的API Key
const BASE_URL = 'https://open.bigmodel.cn/api/paas/v4/chat/completions';

export const gradePaper = async (textContent, imageUrl = null) => {
  const messages = [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: '请批改这份试卷，指出错误并给出正确答案。评分标准要严格。'
        }
      ]
    }
  ];
  
  // 如果有文本内容
  if (textContent) {
    messages[0].content.push({
      type: 'text',
      text: textContent
    });
  }
  
  // 如果有图片
  if (imageUrl) {
    messages[0].content.push({
      type: 'image_url',
      image_url: {
        url: imageUrl
      }
    });
  }
  
  try {
    const response = await axios.post(BASE_URL, {
      model: 'glm-4', // 使用适合的模型
      messages,
      temperature: 0.3, // 降低随机性，使批改更稳定
      max_tokens: 2000
    }, {
      headers: {
        'Authorization': `Bearer ${API_KEY}`,
        'Content-Type': 'application/json'
      }
    });
    
    return response.data;
  } catch (error) {
    console.error('AI批改出错:', error);
    throw error;
  }
};